# player
import pygame

class Player(pygame.sprite.Sprite):
    def __init__(self,pos,constraint,speed):
        super().__init__()
        self.image = pygame.image.load('../graphics/player.png').convert_alpha()
        self.rect = self.image.get_rect(midbottom = pos)
        self.speed = speed
        self.max_x_constraint = constraint
    
    # key control
    def get_input(self):
        keys = pygame.key.get_pressed()

        # K_LEFT / K_RIGHT: normal speed
        # K_LEFT / K_RIGHT + K_SPACE: double speed
        if keys[pygame.K_RIGHT]:
            if keys[pygame.K_SPACE]:
                self.rect.x += self.speed * 2
            else:
                self.rect.x += self.speed
        elif keys[pygame.K_LEFT]:
            if keys[pygame.K_SPACE]:
                self.rect.x -= self.speed * 2
            else:
                self.rect.x -= self.speed

    # moving constraints
    def constraint(self):
        if self.rect.left <= 0:
            self.rect.left = 0
        if self.rect.right >= self.max_x_constraint:
            self.rect.right = self.max_x_constraint

    def update(self):
        self.get_input()
        self.constraint()